package com.hxw.openslplayer;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private boolean isPause = false;

    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       if (ContextCompat.checkSelfPermission(this,
               Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}
                        , 1);

       }
    }

    public void start(View view) {
        String path = "/sdcard/谢春花 - 唱不了一首欢乐的歌.pcm";

        play(path);
    }

    public void pause(View view) {
        isPause = !isPause;
        pause(isPause);
    }

    public void stop(View view) {
        stop();
    }

    /**
     * 开始播放
     * @param url 本地PCM文件的路径（采样率为44100，双声道，16位）
     */
    public native void play(String url);

    /**
     * 暂停/恢复播放
     * @param isPause true：暂停音乐；false：恢复播放
     */
    public native void pause(boolean isPause);

    /**
     * 停止正在播放的音乐
     */
    public native void stop();

    /**
     * 设置音量 0~ 100
     * @param percent
     */
    public native void setVolume(int percent);
}
